package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.DealJsonString;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoAnswerTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

public class DoAnswertmpppppppp2 {


    public static void main(String[] args) throws Exception {



        int sync = 2;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();
        InputStream inputStream2 = new FileInputStream("libs//2022秋//fengx.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];
            LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
            lmsWatchDTO.setUsername(userCode);
            lmsWatchDTO.setPassword(pwd);
        //    if(!"2241001405174".equals(userCode)){
          //  if(!"2144106450255".equals(userCode)){
         //   if(!"2241001405175".equals(userCode)){
            if(!"2137106402515".equals(userCode)
            ){
         //   if(!"2244001401982".equals(userCode)){
          //  if(!"2221001405849".equals(userCode)){
    //        if(!"2251001204872".equals(userCode)){
                continue;
            }

            Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);

            Map<Long, Map<String, AnswerObject>> answers= Maps.newHashMap();
            for(Long courseId:courseMap.keySet()){
                Map<String, AnswerObject> answerMap = initAnswer(courseMap.get(courseId));
                answers.put(courseId,answerMap);
            }
            lmsWatchDTO.setCourseMap(courseMap);
            lmsWatchDTO.setAnswers(answers);
            watchQueue.add(lmsWatchDTO);

        }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoAnswerTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }
    }




    private static Map<String, AnswerObject> initAnswer(String subjectName) throws IOException {
   //     if(!subjectName.contains("形势与政策")){
      //  if(!subjectName.contains("中国")&&!subjectName.contains("形势与政策")){
//        if(!subjectName.equals("实用写作")){
//            return null;
//        }
//        if(!subjectName.contains("实用法律基础")){
//            return null;
//        }
        File file = new File("D:\\project\\answer-tool\\src\\main\\resources\\" + subjectName + "\\" + subjectName + "-answer.json");
        if(!file.exists()){
            return null;
        }
        InputStream inputStream = new FileInputStream(file);
        String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        List<AnswerObject> list = JSONObject.parseArray(answerStr, AnswerObject.class);
        Map<String, AnswerObject> answerMap = Maps.newHashMap();

        boolean isEngCourse;
        isEngCourse = subjectName.contains("英语");
        for (AnswerObject answerObject : list) {
            answerMap.put(answerObject.getQuestionTitle(), answerObject);
            if (isEngCourse) {
                answerMap.put(DealJsonString.removeSpace(answerObject.getQuestionTitle()), answerObject);
            } else {
                answerMap.put(DealJsonString.saveZhWord(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(Util.dealContent(answerObject.getQuestionTitle()), answerObject);
                answerMap.put(DealJsonString.delHTMLTag(Util.dealContent(answerObject.getQuestionTitle())), answerObject);
            }
            if(subjectName.contains("微积分")){
               // originAnswers.put(dealTitleWithHref(title),answerMap.get(title));
                answerMap.put(DoAnswerTask.dealTitleWithHref(answerObject.getQuestionTitle()), answerObject);
            }
        }

        return answerMap;
    }

}
